package com.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author: keke
 * @date: 2021/10/30 20:41
 */
@Configuration
public class TheadPoolConfig {
    private static final Logger LOG = LoggerFactory.getLogger(TheadPoolConfig.class);
    private int coreSize = 5;
    private int maxSize = 10;
    private int queueMaxSize = 10000;

    @Bean("syncPool")
    public ThreadPoolExecutor createThreadPool() {
        ThreadPoolExecutor threadPoolExecutor =
                new ThreadPoolExecutor(coreSize, maxSize, 0L,
                        TimeUnit.MILLISECONDS,
                        new ArrayBlockingQueue<>(queueMaxSize),
                        new NamedThreadFactory("自定义线程池"));
        LOG.info("init thread pool coreSize{},queueMaxSize{},{}", coreSize, maxSize, queueMaxSize);
        return threadPoolExecutor;
    }
}
